﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_NewsTypeDAL
    {
        public List<WI_NewsType> GetAllNewsType()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_NewsType.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.TypeID).AsQueryable();
                List<WI_NewsType> typelist = query.ToList();
                return typelist;
            }
        }

        public List<WI_NewsType> GetAllNewsType(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_NewsType.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.TypeID);
                ttlCount = query.Count();
                List<WI_NewsType> typelist = query.Skip(start).Take(limit).ToList();
                return typelist;
            }
        }

        public WI_NewsType GetNewsType(int typeId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_NewsType ntype = ctx.WI_NewsType.SingleOrDefault(c => c.TypeID == typeId);
                return ntype;
            }
        }

        public void CreateNewsType(NewsType v_ntype)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_NewsType wintype = new WI_NewsType
                {
                    TypeName = v_ntype.TypeName,
                    Description = v_ntype.Description,
                    SortNum = v_ntype.SortNum,
                    CreateTime = DateTime.Now,
                    Creator = v_ntype.Creator
                };
                ctx.WI_NewsType.AddObject(wintype);
                ctx.SaveChanges();
            }
        }

        public bool UpdateNewsType(int typeId, NewsType v_ntype)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_NewsType wintype = ctx.WI_NewsType.SingleOrDefault(c => c.TypeID == typeId);
                if (wintype != null)
                {
                    wintype.TypeName = v_ntype.TypeName;
                    wintype.Description = v_ntype.Description;
                    wintype.SortNum = v_ntype.SortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveNewsType(int typeId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_NewsType wintype = ctx.WI_NewsType.SingleOrDefault(c => c.TypeID == typeId);
                if (wintype != null)
                {
                    ctx.WI_NewsType.DeleteObject(wintype);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
